package com.ruoyi.project.module.osssearch.controller;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.config.AliyunOSSCoreProperties;
import com.ruoyi.project.module.osssearch.model.OssSearch;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.shoukaiseki.apiview.common.util.ByteUtils;
import org.shoukaiseki.apiview.common.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * com.ruoyi.project.module.osssearch.controller.OssSearchController
 * <br>
 *
 * @author 蒋カイセキ    Japan-Tokyo  19-4-17 下午3:33<br>
 * ブログ http://www.shoukaiseki.top/<br>
 * E-メール jiang28555@Gmail.com<br>
 **/
@Controller
@RequestMapping("/module/osssearch")
public class OssSearchController extends BaseController {
    private String prefix = "module/osssearch";


    @Resource
    OSSClient ossClient;

    @Resource
    AliyunOSSCoreProperties aliyunOSSCoreProperties;


    @RequiresPermissions("module:osssearch:view")
    @GetMapping()
    public String osssearch()
    {
        return prefix + "/osssearch";
    }


    @RequiresPermissions("module:osssearch:view")
    @PostMapping("/list")
    @ResponseBody
    public Map list(@RequestParam(name = "nextMarker",required = false)String nextMarker ,
                    @RequestParam(name = "minSize",required = false) Long minSize,
                    @RequestParam(name = "maxSize",required = false) Long maxSize)
    {
        List<OssSearch> list = Lists.newArrayList();
        //每次获取的条数
        int maxkeys=1000;

        String prefix="image";

        String bucketName="orighere";

        ObjectListing objectListing = null;




        // MaxKey默认值为100，最大值为1000。
        ListObjectsRequest request = new ListObjectsRequest(bucketName).withPrefix(prefix)
                .withMaxKeys(maxkeys);

        if(nextMarker!=null){
            request.setMarker(nextMarker);
        }


        objectListing = ossClient.listObjects(request);
        List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
        for (OSSObjectSummary s : sums) {
            boolean b=true;
            long size=s.getSize();
            //超过最小限制,则忽略
            if(minSize!=null){
                if(size<minSize){
                    b=false;
                }
            }
            //超过最大限制,则忽略
            if(maxSize!=null){
                if(size>maxSize){
                    b=false;
                }
            }
            if(b){
                OssSearch ossSearch=new OssSearch();
                ossSearch.setFileName(s.getKey());
                ossSearch.setFileSize(size);
                ossSearch.setFileSizeStr(ByteUtils.byteFormat(size));
                String url = "https://" + bucketName + "." + aliyunOSSCoreProperties.getEndpoint() + "/" + s.getKey();
                ossSearch.setUrl(url);
                ossSearch.setLastModified(s.getLastModified());
                list.add(ossSearch);
            }
        }

        Map map= Maps.newHashMap();
        String nextMarker1 = objectListing.getNextMarker();
        if(!StringUtils.isEmpty(nextMarker1)){
            map.put("nextMarker",nextMarker1);
        }
        map.put("list",list);
        map.put("isTruncated",objectListing.isTruncated());
        map.put("total",sums.size());
        return map;
    }

}
